package com.lijian.community.service.impl;

import com.lijian.community.mapper.UserMapper;
import com.lijian.community.model.User;
import com.lijian.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void insertOrUpdate(User user) {
       User dbUser = userMapper.findByAccountId(user.getAccountId());
       if(dbUser == null){
           //如果没查到用户就做插入
           user.setGmtCreate(System.currentTimeMillis());
           user.setGmtModified(user.getGmtCreate());
           userMapper.save(user);
       } else {
           //否则做更新token
           dbUser.setGmtModified(System.currentTimeMillis());
           dbUser.setPortraitUrl(user.getPortraitUrl());
           dbUser.setName(user.getName());
           dbUser.setToken(user.getToken());
           userMapper.updateByAccountId(dbUser);
       }
    }
}
